<template>
  <div class="home">
    <!-- 头部 -->
    <row-vet></row-vet>
    <!-- 添加收货人地址 -->
    <van-address-edit
      :area-list="areaList"
      :address-info="editList"
      show-set-default
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
    />
  </div>
</template>

<script>
import CityList from './citylist'
import header from '../util/header'
export default {
  data() {
    return {
      areaList: CityList,
      addressArr: [],
      editList: {},
      id: null,
      addressList: []
    }
  },
  components: {
    'row-vet': header
  },
  created() {
    this.addressList = JSON.parse(localStorage.getItem('addressInfo'))
    if (this.$route.query.id) {
      this.id = this.$route.query.id
      const index = this.addressList.findIndex(
        address => address.id === this.id
      )
      this.editList = this.addressList[index]
    }
  },
  methods: {
    onSave(val) {
<<<<<<< HEAD:src/components/address/Address.vue
<<<<<<< HEAD:src/components/address/Address.vue
      if(val.isDefault) {
        Storage.forEach(item => item.isDefault =false)
      }  
      if(this.id) {
=======
 
      if (this.id) {
>>>>>>> address:src/components/address/Address.vue
        // 编辑
        const index = this.addressList.findIndex(add => add.id === this.id)
        
=======
      if (this.id) {
        // 编辑
        const index = this.addressList.findIndex(add => add.id === this.id)
>>>>>>> ec09f62117a1eba362805d662ac97675e4c2f397:src/components/address/Address.vue
        if (index >= 0) {
          // 修改覆盖老数据
          this.addressList[index] = val
          this.addressList[index].id = this.id
        }
        localStorage.setItem('addressInfo', JSON.stringify(this.addressList))
        // const storage1 = JSON.parse(localStorage.getItem('addressInfo'))       
        if (val.isDefault) {
            this.addressList.forEach(item => item.isDefault = false)
            this.addressList[index].isDefault = true
            localStorage.setItem('addressInfo', JSON.stringify(this.addressList))
          }
        // console.log(storage1);
      } else {
        // add
        // 区别于第一次添加
        const arr = []
        const storage = JSON.parse(localStorage.getItem('addressInfo'))
        if (storage) {
          if (val.isDefault) {
<<<<<<< HEAD:src/components/address/Address.vue
            storage.forEach(item => item.isDefault = false)
=======
            Storage.forEach(item => (item.isDefault = false))
>>>>>>> ec09f62117a1eba362805d662ac97675e4c2f397:src/components/address/Address.vue
          }
          let idArr = []
          storage.forEach(item => idArr.push(item.id))
          let nexId = Math.max(...idArr)
          val.id = nexId + 1
          storage.push(val)
          localStorage.setItem('addressInfo', JSON.stringify(storage))
        } else {
          // 第一次添加
          val.id = 1
          const arr = []
          arr.push(val)
          localStorage.setItem('addressInfo', JSON.stringify(arr))
        }
      }

      this.editList = {}
      this.$router.push('/addresslist')

      // if(val.find(item=> item.id === this.id)) {
      //   item.name = this.name
      //   item.tel = this.tel
      //   item.province = this.province
      //   item.city = this.city
      //   item.county = this.county
      //   item.addressDetail = this.addressDetail
      //   item.areaCode = this.areaCode
      //   item.isDefault = this.isDefault

      //   localStorage.setItem('addressInfo', JSON.stringify  (this.addressArr))
      //   localStorage.removeItem('addressInfoEdit')
      //   this.$router.push('/addresslist')
      // } else {
        // if(JSON.parse(localStorage.getItem("addressInfo"))) {
        //   const newStorage = JSON.parse(localStorage.getItem  ("addressInfo"));
        //   const ids = [];
        //   newStorage.forEach(item => ids.push(item.id))
        //   let max = Math.max(...ids)
        //   val.id = ++max
        //   newStorage.push(val)
        //   localStorage.setItem('addressInfo', JSON.stringify  (newStorage))
        //   this.$router.push('/addresslist')
        // } else {
        //   val.id= 1;
        //   this.addressArr.push(val);
        //   localStorage.setItem('addressInfo', JSON.stringify  (this.addressArr))
        //   console.log(val);
        //   this.$router.push('/addresslist')
        // }
      // }
    }
  }
}
</script>

<style lang="less" scoped>
.van-address-edit {
  margin-top: 40px;
}
</style>
